package com.weetop.cfw.base.presenter.imp

import com.weetop.cfw.base.presenter.WorkshopBrowseRecordPresenter
import com.weetop.cfw.base.view.BaseView
import com.weetop.cfw.base.view.WorkshopBrowseRecordView
import com.weetop.cfw.bean.ErrorBean
import com.weetop.cfw.bean.LeaseSaleBrowsingRecordBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.activity.WorkshopBrowseRecordActivity
import com.weetop.cfw.utils.MMKVUtils
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils

class WorkshopBrowseRecordPresenterImp(var workshopBrowseRecordView: WorkshopBrowseRecordView? = null): WorkshopBrowseRecordPresenter {

    /**
     * 获取出租出售浏览记录数据
     * @param curpage 当前数据的页数
     */
    override fun getLeaseSaleBrowsingRecordData(workshopBrowseRecordActivity: WorkshopBrowseRecordActivity, curpage: Int) {
        workshopBrowseRecordActivity.showLoadingDialog(workshopBrowseRecordActivity,"正在加载出租出售浏览记录...")
        RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().getLeaseSaleBrowsingRecord(MMKVUtils.getToken(),curpage),workshopBrowseRecordActivity,object:RxJavaCallBack<LeaseSaleBrowsingRecordBean>(){
            override fun onSuccess(t: LeaseSaleBrowsingRecordBean?) {
                workshopBrowseRecordActivity.dismissLoadingDialog()
                workshopBrowseRecordView?.getLeaseSaleBrowsingRecordDataSuccess(t)
            }
            override fun onErrorResponse(throwable: Throwable?) {
                super.onErrorResponse(throwable)
                workshopBrowseRecordActivity.dismissLoadingDialog()
                workshopBrowseRecordView?.getLeaseSaleBrowsingRecordDataFailed()
            }

            override fun onRequestError(t: LeaseSaleBrowsingRecordBean?) {
                super.onRequestError(t)
                workshopBrowseRecordActivity.dismissLoadingDialog()
                workshopBrowseRecordView?.getLeaseSaleBrowsingRecordDataFailed()
            }

        })
    }

    /**
     * 清空厂房仓库租售浏览记录
     */
    override fun clearBrowsingRecordsOfFactoryBuildingsAndWarehouses(workshopBrowseRecordActivity: WorkshopBrowseRecordActivity) {
       workshopBrowseRecordActivity.showLoadingDialog(workshopBrowseRecordActivity,"正在清空厂房仓库租售浏览记录...")
       RxJavaUtils.useInAppCompatActivityOnDestory(RetrofitUtils.getApi().clearBrowseRecordList(1),workshopBrowseRecordActivity,object:RxJavaCallBack<ErrorBean>(){
           override fun onSuccess(t: ErrorBean?) {
               workshopBrowseRecordActivity.dismissLoadingDialog()
               workshopBrowseRecordActivity.showNativeShortToast("厂房仓库租售浏览记录清除成功")
               workshopBrowseRecordView?.workshopBrowseRecordClearSuccess()
           }

           override fun onRequestError(t: ErrorBean?) {
               super.onRequestError(t)
               workshopBrowseRecordActivity.dismissLoadingDialog()
               workshopBrowseRecordActivity.showNativeShortToast(t?.message)
           }

           override fun onErrorResponse(throwable: Throwable?) {
               super.onErrorResponse(throwable)
               workshopBrowseRecordActivity.dismissLoadingDialog()
           }

       })

    }


    /**
     * 绑定
     */
    override fun attachView(baseView: BaseView) {
        workshopBrowseRecordView = baseView as? WorkshopBrowseRecordView
    }

    /**
     * 解绑
     */
    override fun detachView() {
        workshopBrowseRecordView = null
    }
}